


*** REVISING Progam to link all women (regardless of marital status) and their partners in MIS 4 and 8

/* This program links couples across all MIS using IPUMS longitudinal person-identifiers (CPSIDP). 
(See Drew et al (2014) for more information on these variables.)

Next, these mechanical links are evaluated for consistency on age and race/ethnicity. 
(See Madrian & Lefgren 2000 for more information on demographic checks). As examples, this program includes 
checks over two adjacent MIS (e.g, 3 vs 4) and over two adjacent years (1 vs 5), but these can 
be adapted by researchers for alternative MIS comparisons and match criteria.

References to ONERACE and to race dummies can be removed if you decide not to run the bridging program. 

	

*/

/*This program has 5 parts
	1) create separate files for men and women who are in different-sex partnerships involving the householder
	2) split male and female files into separate files for each month-in-sample (MIS)
	3) link couples within each MIS using cpsidp variables (cpsidpm and cpsidpf)
	4) Technical matches: link couples longidudinally across each MIS using cpsidp
	5) Demographic checks: identify matches on age and race 
*/



/******************
*******************

Set up local macros
User must fill in the paths and file names, identify sex and time-varying variables, 
MIS used in analysis, and comparison MIS for demographic checks. 


*******************
******************/

	* directory
	local indir "../replication-package"

	* datafiles 
	local indata "data/setup/02_empstat.dta" /* identify input data file, e.g. racebridge.dta */
	local linked "data/setup/03_linked_women.dta" /* identify output data file, e.g. racebridge.dta, e.g. linkedcpl_beta.dta */ 

	* select months-in-sample MIS to include in linked files (all months are listed below)
	local keepmonth 4 8 
	
	* NOT relevent for 4-8 comparison
	* identify MIS for demographic checks (will depend on links of interest for research)
		* compare with previous MIS, entering below the MIS+1 month 
			* e.g. to compare MIS 2 to 1 enter 2; for all months list MIS 2-8)
		*local checkprevmo 2 3 4 5 6 7 8 
		
		* compare with previous year, entering below the MIS+4 month 
			*e.g. to compare MIS 5 to 1, enter 5; for all all annual comparisons list MIS 5-8)
		*local checkprevyr 5 6 7 8
		local checkprevyr 8

	* NOT relevent for 4-8 comparison
	* identify MIS for demographic checks (will depend on links of interest for research)
		* compare with previous MIS, entering below the MIS+1 month 
			* e.g. to compare MIS 2 to 1 enter 2; for all months list MIS 2-8)
		*local checkprevmo 2 3 4 5 6 7 8 
		
		* compare with previous year, entering below the MIS+4 month 
			*e.g. to compare MIS 5 to 1, enter 5; for all all annual comparisons list MIS 5-8)
		*local checkprevyr 5 6 7 8
		local checkprevyr 8

	* list of individual partner varying variables so that they can be renamed with gender suffixes (household level variables do not vary between partners)
		* note, variables can be listed individually or in a range, so long as variables are ordered so non-varying variables appear first
		* non-gender varying variables include: cpsid mish month year statefip hwtfinl + other household-level variables
		local varmf cpsidp* relate* sex* pernum-onerace af-workhr82on empstat3  /* list variables here  	*/
		
	
		* MIS suffixes: add suffixes to all variables that can potentially vary by MIS
			* non-time varying variables include: cpsidp cpsidpm cpsidpf statefip and data set is re-ordered below ensure that ensure that non-varying variables appear first
			* having time-varying cpsidpm will allow merges between all women file and partners 

		local varmmis cpsidpm relatem mish month year statefip serial hwtfinl pernumm wtfinlm agem-oneracem afm-workhr82onm empstat3 /* list variables here, should have the suffix m */
		local varfmis cpsidpm relatef mish month year statefip serial hwtfinl pernumf wtfinlf agef-oneracef aff-workhr82onf inunionf samesexf empstat3  /* list variables here, should have the suffix f */
		
		
/******************
*******************
This section:
	1) selects only different-sex couples
	2) splits file into separate files for males and females
	3) adds suffix to indicate gender to all gender-varying variables 

*******************
******************/

clear
set more off

cd "`indir'"


* select women age 16+ regardless of partnership status 
use if sex ==2  & inlist(mish, 4, 8) using "`indata'" , clear


	* keep if age 15+ in MIS 4, will keep all females in MIS 8 since there are some mechanical links that can be made 
	drop if age < 15 & mish == 4

	***** SK check *****
	* variables that can be dropped 
	drop asecflag lnkfw1mwt* lnkfw8wt* lnkfwmis45wt* unmarrpH age10 AIPROB APIPROB BPROB WPROB mrmrace 
	
	* identify different sex couples (inferred relationship, not self reported in most years)
	gen inunionf = sploc > 0 & sploc !=.
	gen samesexf = (inunion == 1 & sex == sex_sp)

	* reorder variables so that linking variables and non-gender varying variables appear first
	order cpsid cpsidp cpsidp_sp relate relate_sp sex sex_sp mish month year statefip
	
	* SKIP because including all women
	* select females in heterosexual relationships
		*keep if sploc >0
		*keep if sex==2 & sex_sp == 1

	* Select relationships involving the head
	*keep if (relate == 101 | relate == 201 | relate == 202 | relate == 1114 | relate == 1116) | (relate_sp == 101)

	* add f subscript to all undividual level variables
	foreach vn of varlist `varmf' {
	rename `vn' `vn'f
	}


	* rename attached characteristics of partners (_sp variables) to reflect partner gender suffixes to facilitate linking and verification
	* cpsidp_sp is generated by IPUMS, rename to cpsidpf 
	rename cpsidp_sp cpsidpm
	rename sex_sp sexm
	rename relate_sp relatem
	
	label var cpsidpm "cpsid, male partner"
	label var cpsidpf "cpsid, female partner"
	label var relatem "relate, male partner"
	label var relatef "relate, female partner"

	
	* Drop duplicative variables
	drop sexm sexf relatem
	
sort cpsidpf cpsidpm 

tempfile tmp_female
save `tmp_female', replace




* select men with a partner (sploc >0)
use if sploc> 0 & sex ==1 & inlist(mish, 4, 8) using "`indata'" , clear


	***** SK check *****
	* variables that can be dropped 
	drop asecflag lnkfw1mwt* lnkfw8wt* lnkfwmis45wt* unmarrpH age10 AIPROB APIPROB BPROB WPROB mrmrace 
	
	* reorder variables so that linking variables and non-gender varying variables appear first
	order cpsid cpsidp cpsidp_sp relate relate_sp sex sex_sp mish month year statefip
		
	* select males in heterosexual relationships
	keep if sploc > 0
	keep if sex==1 & sex_sp == 2
	
	* select relationships involving the head
		*keep if (relate == 101 | relate == 201 | relate == 202 | relate == 1114 | relate==1116) | (relate_sp == 101)

	* add m subscript to all variables except attached characteristics
	foreach vn of varlist `varmf'  {
		rename `vn' `vn'm
	}

	* rename attached characteristics of partners (_sp variables) to reflect partner gender suffixes to facilitate linking and verification
		* cpsidp_sp is generated by IPUMS, rename to cpsidpf 
	rename cpsidp_sp cpsidpf
	rename sex_sp sexf
	rename relate_sp relatef
	
	label var cpsidpm "cpsid, male partner"
	label var cpsidpf "cpsid, female partner"
	label var relatem "relate, male partner"
	label var relatef "relate, female partner"
	
	*** drop duplicative variables
	drop sexm sexf relatef
	
sort cpsidpf cpsidpm 

tempfile tmp_male
save `tmp_male', replace



/******************
*******************
This section uses the male and female files, renames all time-varying with a suffix
indicating which the month-in-sample (MIS) for that obs, and generates a separate data file 
for each MIS

Makes sure to identify relevant MIS in keepmonth macro at top of file
*******************
******************/

* NOTE: will merge on male (partner) data onto female (all marst) file, so need a cpsidpm4 and cpsidpm8 on bothe male and female file, but cpsidf will not vary by   





foreach m in `keepmonth' {
	* select observations in MIS m
	use if mish == `m' using "`tmp_male'" , clear
	
	order cpsid cpsidpf cpsidpm relatem  
	
	* add month suffix to all time-varying variables
	foreach var of varlist `varmmis' {
		rename `var' `var'`m'
	}
	
	gen inmis`m' = 1
	
	sort cpsidpm cpsidpf

	* output data file for males for each MIS
	tempfile tmpm`m'
	save `tmpm`m'', replace	
}

 
foreach m in `keepmonth' {
	* select observations in MIS m
	use if mish == `m' using "`tmp_female'" , clear	
	order cpsid cpsidpf cpsidpm relatef 
	 
	
	foreach var of varlist `varfmis' {
		rename `var' `var'`m'
	}
	
	gen inmis`m' = 1
	
	sort cpsidpm cpsidpf
	tempfile tmpf`m'
	save `tmpf`m'', replace
}





/******************
*******************
This section merges together the female files across MIS 4 and MIS 8 and then matches to the partner file

** Mechanical links **
This section merges couples across MIS based on cpsidpf and and cpsidpm (mechanical matches)

*******************
******************/


use `tmpf4', clear
	
	* merge female files together
	merge 1:1 cpsidpf using `tmpf8'
	
	* generate a flag to identify couples in MIS m
		* inmism = 1 for all because of sample selection criteria 
	
	tab _merge
	
	rename _merge merge_f48
	tab inmis4 inmis8, missing
	
	* drop cases who appear in mis8 only
	drop if merge_f48 == 2

	* flag indicating in inmisf4 (=1 for all) 
	gen inmisf4 = 1 
	
	gen inmisf8 = 1 if merge_f48 == 3
		replace inmisf8 = 0 if merge_f48 == 1
	
	tempfile female_mis48
 	save `female_mis48', replace

* merge male partner records onto female

use `female_mis48', clear
	merge 1:1 cpsidpf using `tmpm4'
	
	tab _merge
	tab inmisf4 _merge, missing

	* drop partnered males who don't link to any women 16+ (typically b/c female parter is < 16)
	drop if _merge == 2
	
	gen matched_sp4 = (_merge==3)
	label var matched_sp4 "Mechanical partner link: MIS4" 
	
	* check linking with thsoe with different sex partners (100% link, as they should)
	tab inunionf4 matched_sp4 if samesexf4 == 0, missing
	tab inunionf4 matched_sp4 if samesexf4 == 1 & inunionf4 == 1, missing
	tab inunionf4 matched_sp4 if inunionf4 == 0, missing
	
	
	drop _merge
	
	* note 2700 women who are married-spouse present don't match, these seem likely to be headless households
		 * there are 80 cases in male file that doesn't match, these look to be women < 16 
		 
		 *  2,169 women are coded as married spouse present but no sploc, these seem to be 1982 and 1983
		 
		 * 8000 cases with sploc > 0 but no partner, these are same sex couples (inferred)
	
	
	merge 1:1 cpsidpf using `tmpm8'
	
	tab _merge
	tab inmisf4 _merge, missing

	* drop partnered males who don't link to any women in MIS4 file 
	drop if _merge == 2
	
	gen matched_sp8 = (_merge==3)
	label var matched_sp8 "Mechanical partner link: MIS8" 

	tab matched_sp8 
	tab inunionf4 matched_sp8 
	
	tab merge_f48 matched_sp8, missing 
	tab merge_f48 matched_sp8 if inunionf4 == 1, missing 
	tab merge_f48 matched_sp8 if inunionf4 == 1, missing 


	* look at cases where F partner doesn't link with her MIS8 record, but matches to a partner in MIS8 -- just a handful who ages dropped below 16 in MIS 8
	list year* month* cpsid* if merge_f48 == 1 & matched_sp8 == 1
	
	drop _merge
	
	
	gen newunionf48 = (inunionf4 == 0 & inunionf8==1)
	
	gen separate48 = (inunionf4 == 1 & inunionf8==0)

	tab1 newunion separate

	tab newunion if inunionf4 == 0 
	tab separate if inunionf4 == 1 

	
	* identify people in new unions: small number in most years, except in 1982 and 1983 where these may reflect be errors in CPS identifiers rather new unions
		* note this reflects mechanical links, and doesn't check for demographic changes, including gender
	gen newpartner48 = 1 if (inunionf4 == 1 & inunionf8 == 1 & cpsidpm4 != cpsidpm8) 	
		replace newpartner48 = 0 if (inunionf4 == 1 & inunionf8 == 1 & cpsidpm4 == cpsidpm8) 		 
		replace newpartner48 = . if (inunionf4 == 0 ) 
	
	label var newpartner48 "New partner ID in MIS 8, women in different sex unions at MIS4"
	
	tab newpartner48, missing
	tab samesexf4 newpartner if inunionf4 == 1 & inunionf8 == 1, missing  	
	
tempfile female_mis48sp 
save `female_mis48sp', replace

		
	
/******************
*******************
** Demographic verification **
This section conducts demographic changes on technical links based on age and race for two comparisons:

	1) comparison to previous MIS, e.g. MIS 1 and 2 and 7 and 8, using local macro checkprevmo (SKIPPED for 4-8)
	2) comparison to previous year, e.g. MIS 1 and 5 and 4 and 8, using local macro checkprevyr

Code can be altered to reflect matches across different time period

Local macro checkprevmo and checkprevyr can be defined in program header 

** Notes on age matches **
Age top codes change over time as follows: 
		1962-1987: 99+ coded as 99 
		1988-2001: 90+ coded as 90 
		2002-2003: 80+ coded as 80 
		2004-onward: 80-84 coded as 80, 85+ coded as 85
		
Madrian and Lefgren (YYYY) suggest allowing age differences between consecutive years of -1 to +3 years
	* Because interviews aren't conducted exactly 12 months apart, a respondent could be 0-2 years older at MIS+4 interview;
		but additional leeway allows for age misreporting 
	* for shorter periods (month to month), "matches" are set at -2 to +2 older at MIS+1, 
		except for the comparison MIS 4-5 where there is a 8-month interval

** Notes on race matches **
Note: in 2003, CPS respondents were allowed to report multiple races
	* additional care should be taken when comparing race between 2002 and 2003
	* code below defines a match if either RACE (original CPS variable), race dummies, or ONERACE (bridge race match)

*******************
******************/	
	

**** START HERE, make sure to update the male match code per ages 	

use `female_mis48sp', clear

	* replace missing inmis variables with zeros (clean this up earlier in the code)
	replace inmis4 = 0 if inmis4 == .
	replace inmis8 = 0 if inmis8 == .
	replace inmisf8 = 0 if inmisf8 == .	
	
	* Age matches vs. previous year 
	foreach m in 8 {
		local l = `m'-4
		gen agediffm_mis`l'`m'=agem`m'-agem`l'	
				replace agediffm_mis`l'`m' = . if (matched_sp`m'==0 | matched_sp`l' == 0)
		/* identify age-matches within -1 to +3 */
		gen agematchm_mis`l'`m' = 1 if (agediffm_mis`l'`m' >=-1 & agediffm_mis`l'`m' <= 3) & agediffm_mis`l'`m' !=. 
			replace agematchm_mis`l'`m' = 0 if (agediffm_mis`l'`m' <-1 | agediffm_mis`l'`m' > 3) & agediffm_mis`l'`m' !=. 

			/* identify age matches when top-coding changes*/
			/* top codes, 2004+: 80=80-84, 85=85+, need to allow 5-year difference in age*/
			replace agematchm_mis`l'`m' = 1 if agem`l'==80 & agem`m'==85 & year`l'>=2004
			/* top codes 2002-2003: 80+, this allow ages to increase from 80 (topcode in 2003) to 85 (top code in 04) between 2003 and 2004 */
			replace agematchm_mis`l'`m' = 1 if agem`l'==80 & agem`m'==85 & year`l'==2003 & year`m'==2004
			/* top codes 1988-2001: 90+ */
			replace agematchm_mis`l'`m' = 1 if agem`l'>=80 & agem`m'==80 & year`l'==2001 & year`m'==2002
			/* top codes 1962-1987: 99+ */
			replace agematchm_mis`l'`m' = 1 if agem`l'>=90 & agem`m'==90 & year`l'==1987 & year`m'==1988
		
			* set to missing if there is no partner in either MIS4 or MIS8
			replace agematchm_mis`l'`m' = . if (matched_sp`m'==0 | matched_sp`l' == 0)

		gen agedifff_mis`l'`m'=agef`m'-agef`l'	
				replace agedifff_mis`l'`m' = . if (inmis`m'==0 | inmis`l' == 0)
		/* identify age-matches within -1 to +3 */
		gen agematchf_mis`l'`m' = 1 if (agedifff_mis`l'`m' >=-1 & agedifff_mis`l'`m' <= 3) & agedifff_mis`l'`m' !=. 
			replace agematchf_mis`l'`m' = 0 if (agedifff_mis`l'`m' <-1 | agedifff_mis`l'`m' > 3) & agedifff_mis`l'`m' !=. 

			/* identify age matches when top-coding changes*/
			/* top codes, 2004+: 80=80-84, 85=85+, need to allow 5-year difference in age*/
			replace agematchf_mis`l'`m' = 1 if agef`l'==80 & agef`m'==85 & year`l'>=2004
			/* top codes 2002-2003: 80+, this allow ages to increase from 80 (topcode in 2003) to 85 (top code in 04) between 2003 and 2004 */
			replace agematchf_mis`l'`m' = 1 if agef`l'==80 & agef`m'==85 & year`l'==2003 & year`m'==2004
			/* top codes 1988-2001: 90+ */
			replace agematchf_mis`l'`m' = 1 if agef`l'>=80 & agef`m'==80 & year`l'==2001 & year`m'==2002
			/* top codes 1962-1987: 99+ */
			replace agematchf_mis`l'`m' = 1 if agef`l'>=90 & agef`m'==90 & year`l'==1987 & year`m'==1988
		replace agematchf_mis`l'`m' = . if (inmis`m'==0 | inmis`l' == 0)		
	
	gen agematch_mis`l'`m' = 1 if agematchf_mis`l'`m'==1 & agematchm_mis`l'`m'==1
		replace agematch_mis`l'`m' = 0 if (agematchf_mis`l'`m'==0 | agematchm_mis`l'`m'==0)
		replace agematch_mis`l'`m' = . if (matched_sp`m'==0 | matched_sp`l' == 0)
		
	
	label var agediffm_mis`l'`m' "Age diff between MIS`l' and MIS`m' (male)"
	label var agedifff_mis`l'`m' "Age diff between MIS`l' and MIS`m' (female)"
	label var agematchm_mis`l'`m' "Age match between MIS`l' and MIS`m' (male)"
	label var agematchf_mis`l'`m' "Age match between MIS`l' and MIS`m' (female)"
	label var agematch_mis`l'`m' "Age match between MIS`l' and MIS`m' (both partners)"
	}
	
	
	
	* Race matches vs. previous MIS (using either detailed race, race dummy, or bridged race
		* beginning in 2003, repondents could report multiple race codes so additional checks are performed between  

	* Race matches vs. previous year (using either detailed race, race dummy, or bridged race
	foreach m in 8 {
		local l = `m'-4
		gen racematchm_mis`l'`m' =  .
			replace racematchm_mis`l'`m' = 1 if ((oneracem`l'==oneracem`m') | (racem`l'==racem`m')) & (matched_sp`m'==1 & matched_sp`l' == 1)
			replace racematchm_mis`l'`m' = 1 if ((whitem`l'==2 & whitem`m' == 2) | (blackm`l'==2 & blackm`m'==2) | (amindm`l'==2 & amindm`m'==2) | /// 
				(apim`l'==2 & apim`m'==2) |(racotherm`l'==2 & racotherm`m'==2)) & (inmis`l' == 1 & inmis`m' ==1) & ///
				year`l' == 2002 & year`m' == 2003 & racematchm_mis`l'`m' ==.
			replace racematchm_mis`l'`m' = 0 if (matched_sp`m'==1 & matched_sp`l' == 1) & racematchm_mis`l'`m' ==.
			
			* set to missing if there is no partner in either MIS4 or MIS8
			replace racematchm_mis`l'`m' = . if (matched_sp`m'==0 | matched_sp`l' == 0)
	
			
		gen racematchf_mis`l'`m' = .
			replace racematchf_mis`l'`m' = 1 if ((oneracef`l'==oneracef`m') | (racef`l'==racef`m')) & (inmis`l' == 1 & inmis`m' ==1)
			replace racematchf_mis`l'`m' = 1 if ((whitef`l'==2 & whitef`m' == 2) | (blackf`l'==2 & blackf`m'==2) | (amindf`l'==2 & amindf`m'==2) | /// 
				(apif`l'==2 & apif`m'==2) |(racotherf`l'==2 & racotherf`m'==2)) & (inmis`l' == 1 & inmis`m' ==1) & ///
				year`l' == 2002 & year`m' == 2003 & racematchf_mis`l'`m' ==.
			replace racematchf_mis`l'`m' = 0 if (inmis`l' == 1 & inmis`m' ==1) & racematchf_mis`l'`m' ==.

		gen racematch_mis`l'`m' = racematchf_mis`l'`m' ==1 & racematchm_mis`l'`m' ==1
			replace racematch_mis`l'`m' = . if (matched_sp`m'==0 | matched_sp`l' == 0)
				
			
		label var racematchm_mis`l'`m' "Match on race between MIS`l' and MIS`m' (male)"
		label var racematchf_mis`l'`m' "Match on race between MIS`l' and MIS`m' (female)"
		label var racematch_mis`l'`m' "Match on race between MIS`l' and MIS`m' (both partners)"
	}		


	
	* create indicators for matches on demographic characteristics and mechanical links
	
	
	* check codes for couples vs individuals
	
	* comparing two adjacent years
	foreach m in 8 {
		local l = `m'-4
		
		* All women: flag to indicate matches between MIS 4 and MIS 8
		gen matchf_mis`l'`m' = 3 if (inmis`l' == 1 & inmis`m' == 1) & agematchf_mis`l'`m'==1 & racematchf_mis`l'`m' == 1 
			replace matchf_mis`l'`m' = 2 if (inmis`l' == 1 & inmis`m' == 1) & (agematchf_mis`l'`m'==0 | racematchf_mis`l'`m' == 0)
			replace matchf_mis`l'`m' = 1 if (inmis`l' == 1 & inmis`m' == 0)

		label define matchmisf`l'`m' 1 "1: In MIS `l' only (female sample)" 2 "2: Mechical link, demog mismatch MIS `l' and `m' (females)" ///
			3 "3: Link and demog match MIS `l' and `m' (females)", replace
		label val matchf_mis`l'`m' matchmisf`l'`m'
		label var matchf_mis`l'`m' "Match on demographics between MIS`l' and MIS`m' (female sample)"
	
			
		* Partners: flag to indicate matches between MIS 4 and MIS 8		
			* same partner at both waves, and age, race, and demographic matches
		gen match_mis`l'`m' = 7 if (inmis`l' == 1 & inmis`m' == 1) & agematch_mis`l'`m'==1 & racematch_mis`l'`m' == 1 & newpartner == 0
			* partners observed at both waves, but age or race mis-match either partner
			replace match_mis`l'`m' = 6 if (inmis`l' == 1 & inmis`m' == 1) & (agematch_mis`l'`m'==0 | racematch_mis`l'`m' == 0) & newpartner == 0
			* partners observed at both waves, but gender mis-match between waves (male p is coded as female in MIS 8)
			replace match_mis`l'`m' = 5 if (inmis`l' == 1 & inmis`m' == 1) & (samesexf`m' ==1) & newpartner == 0		
			* New partner between waves (CPIDPM mismatch)
			replace match_mis`l'`m' = 4 if (inmis`l' == 1 & inmis`m' == 1) & newpartner`l'`m' == 1
			* Separated between waves (female partner in both waves, but partner only in MIS4)
			replace match_mis`l'`m' = 3 if (inmis`l' == 1 & inmis`m' == 1) & separate`l'`m' == 1			
			* In a different-sex partnership at MIS4, but women not observed in second wave
			replace match_mis`l'`m' = 2 if (inmis`l' == 1 & inmis`m' == 0) & inunionf`l' == 1 & samesexf`l' == 0
			* In a same-sex union at MIS 4
			replace match_mis`l'`m' = 1 if (samesexf`l' == 1)	
			* not in a union in MIS 4
			replace match_mis`l'`m' = 0 if (inunionf`l' == 0)
		
		label define matchmis`l'`m' 0 "0: Not in a union at MIS `l' (women)" 1 "1: In a same-sex union at MIS `l' (women)" ///
				2 "2: Not in MIS `m' (women partnered at MIS `l')" 3 "3: Separated between MIS `l' and MIS `m'"  4 "4: Repartnered between MIS `l' and MIS `m'" ///  
				5 "5: Linked to same partner, but gender mismatch" ///
				6 "6: Linked to same partner, but age or race mismatch" 7 "7: Link & demographic match between MIS`l' and MIS`m' (both partners)"
		
		label val match_mis`l'`m' matchmis`l'`m'
		label var match_mis`l'`m' "Match on demographics between MIS`l' and MIS`m' (partners)"
	}
	
	
	
	* universe: lnkfw1ywtf4 is the linking key: lnkfw1ywtf4 == . no links, 0 means linkable, but didn't link,
		* a small number of people who linked received a zero weight which is incorrect (~1000) 
	
	gen linkable_mis48 = (lnkfw1ywtf4 != .)
	label var linkable_mis48 "Eligible to link MIS 4-8"

	gen errorlnkwt = (lnkfw1ywtf4 == 0 & matchf_mis48 > 1)
	label var errorlnkwt "Flag: weight is zero, but R links"
	
	tab linkable errorl, missing
	tab matchf_mis48 linkable, missing
	tab matchf_mis48 errorl, missing
	
save "`linked'", replace	




 
